package com.bjdd.jdy.ui.sign;

import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SignUtils;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.T;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/3.
 */

public class SignPresenter extends SignContract.Presenter {
    @Override
    public void onStart() {
        if (SpUtil.getUser() == null || SpUtil.getUser().equals("") || SpUtil.getUser().getCarrierId() == null) {
            JumpUtil.toLogin(context);
            return;
        }
    }

    @Override
    void sign(String orderNo, String code) {
        showLoadingDialog(false);
        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        Map<String,String> params = new HashMap<>();
        L.d("orderNo:"+orderNo+"--------carrierId:"+carrierId+"--------------code:"+code);
        //params.put("token",token);
        params.put("carrierId",carrierId);
        params.put("orderNo",orderNo);
        params.put("code",code);
        String sign = SignUtils.getSignature(params);
        rxManager.add(model.sign(token,sign,orderNo,carrierId,code)
        .subscribe(result -> {
            hideLoadingDialog();
            if (!result.isError()){
                view.signSuccess();
            }else {
                T.show(result.getMsg());
            }
        },e -> {
            hideLoadingDialog();
            T.show("提交失败");
        }));
    }

    @Override
    void sendSMS(String orderNo, String carrierId) {
        showLoadingDialog(false);
        String token = SpUtil.getToken();
        rxManager.add(model.sendSMSCode(token,orderNo,carrierId)
        .subscribe(result -> {
            hideLoadingDialog();
            if (!result.isError()){
                view.sendSuccess();
            }else {
                T.show(result.getMsg());
            }
        }, e ->{
            hideLoadingDialog();
            T.show("发送失败");
        }));
    }
}
